package com.gemme.gemme.view;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.gemme.gemme.R;
import com.gemme.gemme.wode.GalleryActivity;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

public class XYDGalleryView extends RelativeLayout {
    private Context context;
    @RequiresApi(api = Build.VERSION_CODES.M)
    public XYDGalleryView(Context context) {
        this(context,null);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public XYDGalleryView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public XYDGalleryView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }

    RecyclerView mRecyclerView;
    GalleryActivity.GalleryAdapter galleryAdapter;
    float screen_width;
    float sh_width ;
    int currentPosition = -1;
    private void initView() {
        LayoutInflater.from(context).inflate(R.layout.xyd_gallery_view,this);
        mRecyclerView = findViewById(R.id.recycler);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        new LinearSnapHelper().attachToRecyclerView(mRecyclerView);

        galleryAdapter = new GalleryActivity.GalleryAdapter();
        mRecyclerView.setAdapter(galleryAdapter);
//        new LinearSnapHelper().attachToRecyclerView(mRecyclerView);
        initListener();

    }

    private void initListener() {
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);



            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                // dx>0则表示右滑, dx<0表示左滑, dy<0表示上滑, dy>0表示下滑

                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                //判断是当前layoutManager是否为LinearLayoutManager
                // 只有LinearLayoutManager才有查找第一个和最后一个可见view位置的方法
                if (layoutManager instanceof LinearLayoutManager) {
                    LinearLayoutManager linearManager = (LinearLayoutManager) layoutManager;
                    //获取第一个可见view的位置
                    int firstItemPosition = linearManager.findFirstVisibleItemPosition();

                    //获取最后一个可见view的位置
                    int lastItemPosition = linearManager.findLastVisibleItemPosition();

                    if(lastItemPosition - firstItemPosition == 2){
                        currentPosition = firstItemPosition + 1;
                    }else{
                        currentPosition = firstItemPosition;
                    }
                }

                onScrolledChangedCallback();

            }
        });
    }

    private void onScrolledChangedCallback() {
        View leftView = null;
        View currentView;
        View rightView = null;
        if(currentPosition < 0)
            return;

        if (currentPosition > 0) {
            leftView = mRecyclerView.getLayoutManager().findViewByPosition(currentPosition - 1);
        }
        currentView = mRecyclerView.getLayoutManager().findViewByPosition(currentPosition);
        if (currentPosition < mRecyclerView.getAdapter().getItemCount() - 1) {
            rightView = mRecyclerView.getLayoutManager().findViewByPosition(currentPosition + 1);
        }

        try {
          /*  leftView.setScaleY(0.8f);
            currentView.setScaleY(1.1f);
            rightView.setScaleY(0.8f);*/

            control(leftView);
            control(currentView);
            control(rightView);

        }catch (Exception e){

        }
    }

    private void control(View view) {
        float left = view.getLeft();
        float right = view.getRight();
        System.out.println();

        float view_half = (left + right) / 2;
        float abs = Math.abs(view_half - sh_width);
        if(abs >=  sh_width){
            view.setScaleY(0.8f);
        }else{
            //  540
            float scale_size = 1 - Math.abs((((sh_width - view_half) / sh_width) * (1 - 0.8f)));
            view.setScaleY(scale_size);
        }
    }



}
